function Animation() {
    let isAnimating = false
    let animationLoop = null
    let requestId = null
    let lastTick = 0
    let tickLength = 20
    this.start = function () {
        if (isAnimating === true) return;
        if (animationLoop === null) return;
        requestAnimationFrame(onAnimationFrame);
        isAnimating = true;
    }
    this.stop = function () {
        cancelAnimationFrame(requestId);
        isAnimating = false;
    }
    this.setAnimationLoop = function setAnimationLoop(callback) {
        animationLoop = callback;
    }
    this.setTickLength = function(tickLength_) {
        tickLength = tickLength_
    }
    function onAnimationFrame(time, frame) {
        let nextTick = lastTick + tickLength
        let tickNum = 0
        if (time > nextTick) {
            tickNum = Math.floor((time - lastTick) / tickLength)
        }
        if (tickNum > 10) {
            lastTick = time
        } else {
            for (let j = 0; j < tickNum; ++j) {
                lastTick += tickLength
                animationLoop(time, frame);
            }
        }
        requestId = requestAnimationFrame(onAnimationFrame);
    }
}

export { Animation }